' Template Provided By
' Code Endeavors, LLC - http://www.codeendeavors.com

Imports System.Web.Script.Serialization
Imports DotNetNuke
Imports DotNetNuke.Common
Imports DotNetNuke.UI.Utilities

Namespace Modules.Quicksand

    '-------------------------------------------------------------------------------------------------------------
    '- The ControlMethodClass attribute identifies the class as supporting Control Methods
    '- The Friendly name parameter is usually named after server-side class namespace, but could be shortened if you like
    '- If you change this make sure you also change its client calling code found in EditQuicksand.ascx.js
    '-   dnn.xmlhttp.callControlMethod('OliverHine.Modules.Quicksand.EditQuicksand.' + this._ns, 
    '-------------------------------------------------------------------------------------------------------------
    <ControlMethodClass("OliverHine.Modules.Quicksand.EditQuicksand")> _
    Partial Class EditQuicksand
        Inherits AjaxPortalModuleBase

#Region "AjaxPortalModuleBase Events"
        '-------------------------------------------------------------------------------------------------------------
        '- This is where you add your custom properties that are to be sent down to the client-side object
        '- By default we are passing the naming container ID (ns) and localized messages (msgs) 
        '- If you add your own properties make sure you update your client code with those properties found in EditQuicksand.ascx.js
        '-      get_ImagePath: function() {return this._imagePath;},
        '-      set_ImagePath: function(value) {this._imagePath = value;},
        '-------------------------------------------------------------------------------------------------------------
        Protected Sub AjaxPortalModuleBase_AddScriptComponentDescriptors(ByVal Descriptor As ScriptComponentDescriptor) Handles MyBase.AddScriptComponentDescriptors
            'IMPORTANT!  Enter Client Namespace + ObjectName as Type
            Descriptor.Type = "OliverHine.EditQuicksand"

            '---------------------------------------------------------------------------------------------------------
            'Add custom properties here
            '   Descriptor.AddScriptProperty("ImagePath", String.Format("'{0}'", Me.ModulePath & "images/"))

            If Not Settings Is Nothing Then
                'This sample uses the Module Settings collection as a custom property serialized to JSON
                Dim jss As JavaScriptSerializer = New JavaScriptSerializer()
                Descriptor.AddScriptProperty("settings", jss.Serialize(Settings))
            End If
            '---------------------------------------------------------------------------------------------------------
        End Sub

        '-------------------------------------------------------------------------------------------------------------
        '- This is where your client-side javascript that uses the MS AJAX framework needs to be registered
        '- Adding the reference here ensures that the MS AJAX script is run before our script which uses things like Type.registerNamespace run
        '-------------------------------------------------------------------------------------------------------------
        Protected Sub AjaxPortalModuleBase_AddScriptReferences(ByVal References As List(Of ScriptReference)) Handles MyBase.AddScriptReferences
            References.Add(New System.Web.UI.ScriptReference(Me.ControlPath & "EditQuicksand.ascx.js"))
        End Sub

        '-------------------------------------------------------------------------------------------------------------
        '- Add any localized text needed on the client
        '-------------------------------------------------------------------------------------------------------------
        Protected Sub AjaxPortalModuleBase_AddLocalizedMessages(ByVal Messages As Dictionary(Of String, String)) Handles MyBase.AddLocalizedMessages
            Messages("SettingsSaved") = Localization.GetString("SettingsSaved.Client", LocalResourceFile)
        End Sub

#End Region

#Region "Event Handlers"

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try
                If Page.IsPostBack() = False Then
                    Dim portalTabs As List(Of Entities.Tabs.TabInfo) = TabController.GetPortalTabs(PortalId, DotNetNuke.Common.Utilities.Null.NullInteger, False, True)

                    ' Add host tabs
                    If UserInfo IsNot Nothing AndAlso UserInfo.IsSuperUser Then
                        Dim hostTabs As TabCollection = New TabController().GetTabsByPortal(Null.NullInteger)
                        portalTabs.AddRange(hostTabs.Values)
                    End If

                    ParentTabDropDownList.Items.Clear()
                    For Each t As Entities.Tabs.TabInfo In portalTabs
                        If (Security.Permissions.TabPermissionController.CanViewPage(t)) Then
                            ParentTabDropDownList.Items.Add(New ListItem(t.IndentedTabName, t.TabID))
                        End If
                    Next
                    ParentTabDropDownList.Items.Insert(0, New ListItem("<Root>", "-1"))
                    ParentTabDropDownList.Items.Insert(1, New ListItem("<Active Tab>", ""))

                    SelectListItem(ParentTabDropDownList, "ParentTabID")

                    If Settings.ContainsKey("ShowHiddenTabs") Then
                        ShowHiddenTabsCheckbox.Checked = Boolean.Parse(Settings("ShowHiddenTabs").ToString)
                    End If
                End If

                cmdCancel.NavigateUrl = NavigateURL()
            Catch exc As Exception        'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

#End Region

#Region "Control Methods"
        '-------------------------------------------------------------------------------------------------------------
        '- Exposing methods to the client code is as simple as adding the ControlMethod attribute
        '- IMPORTANT!   ALWAYS SECURE YOUR CONTROL METHODS IF INTERACTING WITH SENSITIVE DATA
        '-------------------------------------------------------------------------------------------------------------
        <ControlMethod()> _
        Public Function UpdateSettings(ByVal Settings As Dictionary(Of String, Object)) As Boolean
            If Me.EditMode Then 'Making sure only those users with Edit Mode rights can call this method
                Dim modController As DotNetNuke.Entities.Modules.ModuleController = New DotNetNuke.Entities.Modules.ModuleController()
                For Each key As String In Settings.Keys
                    modController.UpdateModuleSetting(Me.ModuleId, key, Settings(key))
                Next
            End If
            Return True
        End Function
#End Region


        Private Sub SelectListItem(ByRef ddl As DropDownList, ByVal key As String)

            If Settings.ContainsKey(key) Then
                ddl.ClearSelection()

                Dim selItem As ListItem
                selItem = ddl.Items.FindByValue(CType(Settings(key), String))

                If Not selItem Is Nothing Then
                    selItem.Selected = True
                End If
            Else
                'can't find match, but lets select the 2nd item as the default
                ddl.SelectedIndex = 1
            End If

        End Sub

    End Class

End Namespace